//
//  AppDelegate.m
//  SNS
//
//  Created by  on 11-11-8.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"
#import "SinaManager.h"
#import "HomeController.h"
#import "SettingController.h"
#import "ImageController.h"
#import "VideoController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize tabBarController;

//+ (UINavigationController *)mainNavigationController {
//    return ((AppDelegate *)[UIApplication sharedApplication].delegate).navigationController;
//}

- (void)dealloc
{
    [_window release];
    [tabBarController release];
    
    [super dealloc];
}

//for ios version below 4.2
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
	if( [[SinaManager sharedManager].sinaWeiBo handleOpenURL:url] )
		return TRUE;
    
	return TRUE;
}

//for ios version is or above 4.2
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
	if( [[SinaManager sharedManager].sinaWeiBo handleOpenURL:url] ) {
        return TRUE;
    }
	
	return TRUE;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    
    self.tabBarController = [[UITabBarController alloc] init];
    
    HomeController *homeController = [[[HomeController alloc] init] autorelease];
    UINavigationController *homeNavigation = [[[UINavigationController alloc] 
                                                initWithRootViewController:homeController] autorelease];
    homeNavigation.navigationBar.barStyle = UIBarStyleBlack;
    homeNavigation.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"信息流"
                                                               image:[UIImage imageNamed:@"home.png"]
                                                                 tag:0] autorelease];
    
    ImageController *imageController = [[[ImageController alloc] init] autorelease];
    UINavigationController *imageNavigatioin = [[[UINavigationController alloc] 
                                                 initWithRootViewController:imageController] autorelease];
    imageNavigatioin.navigationBar.barStyle = UIBarStyleBlack;
    imageNavigatioin.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"图片"
                                                                 image:[UIImage imageNamed:@"photo.png"]
                                                                   tag:1] autorelease];
    
    SettingController *settingController = [[[SettingController alloc] init] autorelease];
    UINavigationController *settingNavigation = [[[UINavigationController alloc] 
                                                 initWithRootViewController:settingController] autorelease];
    settingNavigation.navigationBar.barStyle = UIBarStyleBlack;
    settingNavigation.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"设置"
                                                                  image:[UIImage imageNamed:@"setting.png"]
                                                                    tag:2] autorelease];
    
//    VideoController *videoController = [[[VideoController alloc] init] autorelease];
//    UINavigationController *videoNavigation = [[[UINavigationController alloc] 
//                                                  initWithRootViewController:videoController] autorelease];
//    videoNavigation.navigationBar.barStyle = UIBarStyleBlack;
//    videoNavigation.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMore
//                                                                               tag:3] autorelease];
    
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:
                                             homeNavigation,
                                             imageNavigatioin, 
//                                             videoNavigation,
                                             settingNavigation,
                                             nil];
    
    self.window.rootViewController = self.tabBarController;

    if (![[SinaManager sharedManager].sinaWeiBo isUserLoggedin]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"登录新浪微博账号"
                                                        message:@"本程序基于新浪微博账号,请先登录"
                                                       delegate:self
                                              cancelButtonTitle:nil
                                              otherButtonTitles:@"登录", nil];
        [alert show];
        [alert release];
    }
    
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{

}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
  
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
   
}

- (void)applicationWillTerminate:(UIApplication *)application
{
   
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != alertView.cancelButtonIndex) {
        [[SinaManager sharedManager] login];
    }
}

@end
